  namespace CRM_Perf_BenchMark
    {
    	using System;
    	using System.Collections.Generic;
  	    using Microsoft.VisualStudio.TestTools.WebTesting;
    	using System.Net;
    	using System.Text;
    	using System.Xml;    	
    
      /// <summary>
      /// 1. Start at Service Calendar homepage
	  /// 2. Click Schedule
	  /// 3. Select Service
	  /// 4. Click Find Available Times button
      /// </summary>
      
    	public class SearchAvailability : CrmWebTest
    	{
    		public void SearchAvailability_PreWebTest(object sender, PreWebTestEventArgs e)
    		{
                Guid orgId = EntityManager.Instance.GetRandomOrg();

                EntityRequest er = new EntityRequest();
                er.Type = EntityNames.Users;
                er.ReturnAs = EntityNames.Users;
                er.ParentID = orgId;

                System.Collections.Hashtable userProps = new System.Collections.Hashtable();
                if (e.WebTest.Context.ContainsKey("UserRole"))
                {
                    userProps.Add("role", e.WebTest.Context["UserRole"]);
                    er.Props = userProps;
                }
                
                System.Collections.Hashtable Entities = EntityManager.Instance.GetEntities(er);

                user = (CRMEntity)Entities[EntityNames.Users];

                e.WebTest.UserName = user["domainName"];
                e.WebTest.Password = user["userpassword"];
                
                crmServer = user["CrmServer"];
    
    			service = EntityManager.Instance.GetNextORGEntity(EntityNames.Service,orgId);
    			EntityManager.Instance.FreeEntity(service);                
    		}

    		public void SearchAvailability_PostWebTest(object sender, PostWebTestEventArgs e)
    		{
    			EntityManager.Instance.FreeEntity(user);
    		}    
    
    		public SearchAvailability()
    		{
    			WebRequest.RegisterPrefix("crm", new crmRequestFactory());
    
    			this.Proxy = null;
    			PreWebTest += new EventHandler<PreWebTestEventArgs>(SearchAvailability_PreWebTest);
    			PostWebTest += new EventHandler<PostWebTestEventArgs>(SearchAvailability_PostWebTest);
    		}
  
    
    		public override IEnumerator<WebTestRequest> GetRequestEnumerator()
    		{
  			    gridXml = GridXmlHelper.ServicePage(service[EntityIDNames.Service]);
  
    			this.BeginTransaction("SearchAvailability");
                activities_serviceappointment_edit_aspx actsae = new activities_serviceappointment_edit_aspx(user);
    			actsae._StartTime = DateTime.Now;
    			actsae.loadDialog = true;
    			yield return (actsae);

                referer = actsae.lastResponse.ResponseUri.ToString();

                _forms_FormScript_js_aspx fs = new _forms_FormScript_js_aspx(user);
                fs.etc = 4214;
                fs.FormType = "main";
                fs.FormName = "crmForm";
                headerItem = new WebTestRequestHeader("referer", referer);
                fs.Headers.Add(headerItem);
                yield return (fs);

                _controls_notes_notesdata_aspx note = new _controls_notes_notesdata_aspx(user);
    			note.ParentEntity = 4214;    			
    			note.EnableInlineEdit = true;
    			note.EnableInsert = true;
    			yield return (note);

                sm_activityscheduling_schedulingdialog_aspx smactsch = new sm_activityscheduling_schedulingdialog_aspx(user);
    			smactsch.ActivityTypeCode = 4214;
    			yield return (smactsch);

                _controls_lookup_lookupsingle_aspx lookupsingle = new _controls_lookup_lookupsingle_aspx(user);
    			lookupsingle.Class = "ServicesEnabled";
    			lookupsingle.objecttypes = "4001";
    			lookupsingle.browse = 0;
  			    lookupsingle.DefaultType = 0;
                lookupsingle.ShowNewButton = 1;
                lookupsingle.ShowPropButton = 1;
    			yield return (lookupsingle);

                _controls_lookup_lookupdialogs_css_aspx clld = new _controls_lookup_lookupdialogs_css_aspx(user);
                clld.lcid = 1033;
                yield return (clld);

    			ServiceWebService.ServiceWebService serviceweb = new ServiceWebService.ServiceWebService();
    			CrmRequest wtr = null;
    
    			try { serviceweb.Retrieve(new Guid(service["serviceid"]), new string[] {"duration"}); }
    			catch (crmException x)
    			{
    				wtr = (CrmRequest)x.wtr;
                    wtr.Url = Utils.ReplaceServerName(wtr.Url, user);
    			}
    			yield return (wtr);
    
    			AppGridWebService.AppGridWebService agws = new AppGridWebService.AppGridWebService();
    			WebTestRequest wtrRefresh = null;
    			try { agws.Refresh(gridXml); }
    			catch (crmException x)
    			{
    				wtrRefresh = x.wtr;
                    wtrRefresh.Url = Utils.ReplaceServerName(wtrRefresh.Url, user);
    			}
    			yield return (wtrRefresh);
    
    			this.EndTransaction("SearchAvailability");
    		}

            private string referer;
            private WebTestRequestHeader headerItem;
       		private CRMEntity user;
    		private CRMEntity service;
    		private string gridXml;
            private string crmServer;
    	}
    }